// pages/module/rebate/rebate.js
import regeneratorRuntime from '../../../utils/runtime.js';
import img_list from '../../../utils/util.js';
import time from '../../../utils/time.js';
import { getData, setData, clearData, dataKey } from '../../../utils/storage.js';
import shareFun from '../../../utils/shareFun.js';
import {
  orderList
} from '../../../utils/imapi.js';
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabIndex:0,
    sliderLeft:45,
    sliderOffset:130,
    is_screen:0,
    height:app.globalData.screenHeight,
    tabs:[
      {"id":'',"name":"全部","is_selected":1,status_id:[]},
      {"id":'-1',"name":"待支付","is_selected":1,status_id:[-1]},
      {"id":0,"name":"待结算","is_selected":1,status_id:[0,1,2,3,5]},
      {"id":9,"name":"已结算","is_selected":1,status_id:[9]},
      {"id":8,"name":"已失效","is_selected":1,status_id:[4,8]},
    ],
    order_list:[],
    btn_list1:[
      {"id":1,"name":"全部","p":"","is_selected":1,"p":""},
      {"id":2,"name":"京东","p":"JD","is_selected":0,"p":"JD"},
      {"id":3,"name":"淘宝","p":"TB","is_selected":0,"p":"TB"},
      {"id":4,"name":"拼多多","p":"PDD","is_selected":0,"p":"PDD"},
      {"id":5,"name":"星品","p":"XP","is_selected":0,"p":"XP"},
	  {"id":6,"name":"唯品会","p":"XP","is_selected":0,"p":"vIP"},
    ],
    btn_list2:[
      {"id":1,"name":"全部","p":"","is_selected":1,commission_src:'0'},
      {"id":2,"name":"自购","p":"","is_selected":0,commission_src:'1'},
      {"id":3,"name":"推广","p":"","is_selected":0,commission_src:'2'},
    ],
    commission_src_index:0, //收益来源

    img_list:"img_list.img_list",
    id:0,
    page_index:1,
    status:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:async function (options) {
    this.setData({
      img_list:img_list.img_list
    })
    shareFun.hidenShareMenu();
   
    this.loadData(1)
  },
  loadData: async function(page_index){
    let commission_src=this.data.btn_list2[this.data.commission_src_index].commission_src
    const {data} = await orderList({uid:app.globalData.userInfo.uid,page_index:page_index,commission_src:commission_src,status:this.data.status})//app.globalData.uid
    var timestamp = Date.parse(new Date());
    var today = time.dateformat(timestamp)
    this.setData({
      order_list: this.data.order_list.concat(data),
      today:today.y + '-' + today.m + '-' + today.d,
      page_index:page_index
    })
	  console.log(this.data.order_list);
  },
  screenOrder(){
    this.setData({
      is_screen:!this.data.is_screen
    })
  },
  async onTabClick(e){
    var index = e.detail.index
    const {data} = await orderList({uid:app.globalData.userInfo.uid,status:this.data.tabs[index].id})//app.globalData.uid
    this.setData({
      order_list:data,
      status:this.data.tabs[index].id,
    })
  },
  bindDateChange1: function(e) {
    this.setData({
      start_date: e.detail.value
    })
  },
  bindDateChange2: function(e) {
    this.setData({
      end_date: e.detail.value
    })
  },
  chooseFromOrder(e){
	  console.log(e);
    var index = e.currentTarget.dataset.index;
    this.data.btn_list1.forEach(element => {
      element.is_selected = 0
    });
    this.data.btn_list1[index].is_selected = 1
    this.setData({
      btn_list1: this.data.btn_list1,
	  id:index
    })
  },
  chooseFromEarn(e){
    var index = e.currentTarget.dataset.index;
    this.data.btn_list2.forEach(element => {
      element.is_selected = 0
    });
    this.data.btn_list2[index].is_selected = 1
    this.setData({
      btn_list2: this.data.btn_list2,
      commission_src_index:index
    })
  },
  reset(){
    this.data.btn_list1.forEach(element => {
      element.is_selected = 0
    });
    this.data.btn_list1[0].is_selected = 1
    this.data.btn_list2.forEach(element => {
      element.is_selected = 0
    });
    this.data.btn_list2[0].is_selected = 1
    this.setData({
      start_date:'',
      end_date:'',
      btn_list1:this.data.btn_list1,
      btn_list2:this.data.btn_list2,
    })
  },
  async confirm(){
    let commission_src=this.data.btn_list2[this.data.commission_src_index].commission_src
	  const {data} = await orderList({uid:app.globalData.userInfo.uid,p:this.data.btn_list1[this.data.id].p,start_time:this.data.start_date,end_time:this.data.end_date,commission_src:commission_src})//app.globalData.uid
	  //console.log(data);
    this.setData({
      is_screen:false,
	    order_list:data
    })
  },
  hideScreen(){
    this.setData({
      is_screen:false,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      order_list:[],
      page_index:1
    })
    this.loadData()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

      // this.setData({
      //   page_index:this.data.page_index+1
      // })

      this.loadData(this.data.page_index+1)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})